package edu.colorado.karl.databases;

import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.JFreeChart; 
import de.laures.cewolf.ChartPostProcessor; 

/**
 * ChartAxiesSetter sets up the time axis to show the complete selected
 * time range, rather than just where data is.  Also sets the date label
 * to a compact but complete date-timestamp.
 * 
 * @author Eric
 * @version 4/30/08
 */
public class ChartAxiesSetter implements ChartPostProcessor {

	/**
	 * Sole constructor.
	 */
	public ChartAxiesSetter() {
		
	}
	
	/**
	 * Changes the date format displayed on the x axis to "M-d-yy  H:mm" and
	 * sets the start and end dates of the chart to the selected date range.
	 * @param chart the chart being processed
	 * @param params start and end date parameters passed from the jsp 
	 */
	public void processChart(Object chart, Map params) { 
		SimpleDateFormat format =  new SimpleDateFormat("M-d-yy  H:mm");
		XYPlot plot = (XYPlot)((JFreeChart) chart).getPlot(); //cast to XYPlot
		DateAxis xAxis = (DateAxis)plot.getDomainAxis();
		
		xAxis.setDateFormatOverride(format);
		
		if(params.get("startDate") != null) { //if valid date param passed from jsp
			xAxis.setMinimumDate((Date) params.get("startDate"));
		}
		if(params.get("endDate") != null) { //if valid date param passed from jsp
			xAxis.setMaximumDate((Date) params.get("endDate"));
		}
	}
}
